<template>
  <div class="modal-location">
    <van-nav-bar fixed title="" left-arrow :border="false" @click-left="handleClose" />
    <div class="title">Select Location</div>
    <div class="list" v-if="!detail.code">
      <div class="item" v-for="(item, index) in locationList" :key="index" @click="handleDetail(item)">
        <div class="center">{{ item.name }}</div>
        <div class="right">
          <van-icon name="arrow" size="20"></van-icon>
        </div>
      </div>
    </div>
    <div class="detail-list" v-if="detail.code">
      <div class="sec-list">
        <div class="sec-block" v-if="showAll">
          <div class="sec-item" v-if="detail.code" @click="handleCheck()">
            <div class="sec-left">Tudo em {{ detail.name }}</div>
          </div>
          <div class="sec-parent">
            {{ detail.name }}
          </div>
        </div>
        <div class="sec-block" v-for="(sec, index) in detail.children" :key="index" @click="handleCheck(sec)">
          <div class="sec-item">
            <div class="sec-left">{{ sec.name }}</div>
            <div class="sec-right" v-if="cityCode === sec.code">
              <van-icon name="success" size="20" color="#fb8c24"></van-icon>
            </div>
          </div>
          <div class="sec-parent">
            {{ detail.name }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Api from "@/api";
import { getImgUrl } from "@/utils";
import { Domain } from "@/constants";
export default {
  name: "ModalLocation",
  props: {
    pCode: {
      type: [Number, String],
      default: "",
    },
    cCode: {
      type: [Number, String],
      default: "",
    },
    showAll:{
      type:Boolean,
      default:true
    }
  },
  data() {
    return {
      locationList: [],
      provinceCode: "",
      cityCode: "",
    };
  },
  created() {
    this.provinceCode = this.pCode;
    this.cityCode = this.cCode;
    this.loadTree();
  },
  computed: {
    detail() {
      return this.locationList.find((item) => item.code === this.provinceCode) || {};
    },
  },
  methods: {
    handleClose() {
      if (this.detail.code) {
        this.provinceCode = "";
      } else {
        this.$emit("close");
      }
    },
    loadTree() {
      Api.areaTree().then((res) => {
        this.locationList = res;
      });
    },
    handleDetail(item) {
      this.provinceCode = item.code;
    },
    handleCheck(item) {
      if (item) {
        this.$emit("confirm", {
          provicenCode: this.detail.code,
          cityCode: item.code,
          provinceName: this.detail.name,
          cityName: item.name,
        });
      } else {
        this.$emit("confirm", {
          provicenCode: this.detail.code,
          cityCode: "",
          provinceName: this.detail.name,
          cityName: "",
        });
      }
    },
  },
};
</script>
<style lang="less" scoped>
.modal-location {
  position: fixed;
  left: 0;
  top: 0;
  background: #fff;
  z-index: 1000;
  width: 100%;
  height: 100vh;
  overflow-y: auto;
  padding-top: 50px;
  box-sizing: border-box;
  .title {
    font-weight: bold;
    font-size: 28px;
    color: #000;
    padding: 16px 16px 0;
  }
  .list {
    box-sizing: border-box;
    .item {
      padding: 16px 10px;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #ededed;
      .center {
        flex: auto;
        width: 0;
        margin: 0 15px;
        font-size: 16px;
        color: #333;
      }
      .right {
        flex: none;
      }
    }
  }
  .detail-list {
    padding: 16px;
    box-sizing: border-box;
    .item {
      margin-bottom: 10px;
      padding: 8px;
      display: flex;
      align-items: center;
      border-radius: 6px;
      .left {
        flex: none;
        img {
          width: 48px;
          height: 48px;
          display: inline-block;
          background: #f2f2f2;
          border-radius: 100%;
        }
      }
      .center {
        flex: auto;
        width: 0;
        margin: 0 15px;
        font-size: 16px;
        color: #333;
      }
      .right {
        flex: none;
      }
    }
    .sec-list {
      .sec-block {
        padding: 16px 0;
        border-bottom: 1px solid #ededed;
        box-sizing: border-box;
        .sec-parent {
          color: #999;
          font-size: 14px;
          margin-top: 5px;
        }
        .sec-item {
          display: flex;
          align-items: center;
          .sec-left {
            flex: auto;
            width: 0;
          }
          .sec-right {
            margin-left: 10px;
            flex: none;
          }
        }
      }
    }
  }
}
</style>
